﻿//https://leetcode.cn/problems/happy-number/
class Solution {
public:
    int sum(int n)
    {
        int ret = 0;
        while (n)
        {
            ret += (n % 10) * (n % 10);
            n /= 10;
        }
        return ret;
    }
    bool isHappy(int n) {
        set<int> buffer;

        while (buffer.count(n) == 0)
        {

            buffer.insert(n);
            n = sum(n);
        }
        if (n == 1)return true;
        return false;
    }
};
//双指针
class Solution
{
public:
	int bitSum(int n) // 返回 n 这个数每⼀位上的平⽅和{
		int sum = 0;
	while (n)
	{
		int t = n % 10;
		sum += t * t;
		n /= 10;
	}
	return sum;
}
bool isHappy(int n)
{
	int slow = n, fast = bitSum(n);
	while (slow != fast)
	{
		slow = bitSum(slow);
		fast = bitSum(bitSum(fast));
	}
	return slow == 1;
}
};